package org.iteam.commons.utils;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 邮件发送工具类
 * 
 * @author 王维
 * 
 */
public class EmailUtil {

	// 邮件帐号
	private static String USER = "791404861@qq.com";// 发送邮件人
	private static String BASE_PATH = "http://";
	private static String PWD = "arts19920223!!";// 密码
	private static String SMTP = "smtp.qq.com";// smtp 邮件服务器
	private static String SENDFROM = "791404861@qq.com";// 显示发送人邮件地址
	private static final String SUBJECT = "邮箱激活信息";//
	private static final String ADMIN_ADD = "webmaster@smel.cn";
	private static final String MSG = "请点击以下链接激活中小企业信息化资源平台帐号,%s";

	private static final String HTML = "<style>body,td,pre{font-family:Arial, Verdana, sans-serif}body{font-size:14px;line-height:25px;padding:8px 10px;margin:0;}"
			+ "pre {white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;}"
			+ ".rm_line{border-top:2px solid #F1F1F1; font-size:0; margin:15px 0}"
			+ ".atchImg img{border:2px solid #c3d9ff;}"
			+ ".lnkTxt{ color:#0066CC}"
			+ ".rm_PicArea *{ font-family:Arial, sans-serif; font-size:14px;font-weight:700;}"
			+ ".fbk3{ color:#333; line-height:160%}"
			+ ".fTip{ font-size:11px; font-weight:normal}"
			+ "</style>"
			+ "<pre>\n"
			+ "亲爱的用户{name}：您好！<br/>"
			+ "    感谢您注册中小企业信息化资源平台，您只需要点击下面链接，激活您的帐户,您便可以享受平台的各项服务。<br/>"
			+ "<a href=\"{url}\">{url}</a><br/>"
			+ "  (如果无法点击该URL链接地址，请将它复制并粘帖到浏览器的地址输入框，然后单击回车即可)<br/>"
			+ " <!-- 注意:请您在收到邮件3天内进行激活，否则该激活码将会失效。 请您在90天内进行帐户激活，否则您的帐户将会失效。--><br/>"
			+ "  用户服务支持：" + ADMIN_ADD + "<br/>" + "</pre>";
	private static final String PASSWORD_HTML = "<style>body,td,pre{font-family:Arial, Verdana, sans-serif}body{font-size:14px;line-height:25px;padding:8px 10px;margin:0;}"
			+ "pre {white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;}"
			+ ".rm_line{border-top:2px solid #F1F1F1; font-size:0; margin:15px 0}"
			+ ".atchImg img{border:2px solid #c3d9ff;}"
			+ ".lnkTxt{ color:#0066CC}"
			+ ".rm_PicArea *{ font-family:Arial, sans-serif; font-size:14px;font-weight:700;}"
			+ ".fbk3{ color:#333; line-height:160%}"
			+ ".fTip{ font-size:11px; font-weight:normal}"
			+ "</style>"
			+ "<pre>\n"
			+ "亲爱的用户{name}：您好！<br/>"
			+ "    感谢您使用中小企业信息化资源平台，系统已经为您初始化密码，请注意及时修改密码。<br/>"
			+ " 新密码:{password}"
			+ " <br/>"
			+ "  用户服务支持："
			+ ADMIN_ADD
			+ "<br/>"
			+ "</pre>";
	private static Logger log = LoggerFactory.getLogger(EmailUtil.class);

	public void init(String smtp, String user, String pwd, String sendfrom) {
		EmailUtil.USER = user;
		EmailUtil.SMTP = smtp;
		EmailUtil.PWD = pwd;
		EmailUtil.SENDFROM = sendfrom;
	}

	public static void sendActiveEmail(String email, String name,
			String activecode) {
		sendHtmlEmail(email, name, BASE_PATH + activecode);
	}

	public static void sendPasswordEmail(String email, String name,
			String password) {
		sendPasswordHtmlEmail(email, name, password);
	}

	/**
	 * 文本邮件发送
	 * 
	 * @param sendTO
	 *            接收者邮箱地址
	 * @param url
	 *            激活链接地址
	 */
	public static void sendTextEmail(String sendTO, String url) {
		try {
			Email email = new SimpleEmail();
			email.setDebug(true);
			// email.setSmtpPort(587);
			email.setAuthentication(USER, PWD);
			email.setDebug(false);
			email.setHostName(SMTP);
			email.setFrom(SENDFROM);
			email.setSubject(SUBJECT);
			email.setMsg(String.format(MSG, url));
			email.addTo(sendTO);
			email.setTLS(true);
			email.setCharset("GBK");
			email.send();
			log.info("激活邮件发送成功 [TO:" + sendTO + "]");
		} catch (EmailException e) {
			e.printStackTrace();
			log.error("激活邮件发送失败！");
		}
	}

	public static void sendTextMsgEmail(String sendTO, String msg) {
		try {
			Email email = new SimpleEmail();
			email.setDebug(true);
			// email.setSmtpPort(587);
			email.setAuthentication(USER, PWD);
			email.setDebug(false);
			email.setHostName(SMTP);
			email.setFrom(SENDFROM);
			email.setSubject(SUBJECT);
			email.setMsg(msg);
			email.addTo(sendTO);
			email.setTLS(true);
			email.setCharset("GBK");
			email.send();
			log.info("激活邮件发送成功 [TO:" + sendTO + "]");
		} catch (EmailException e) {
			e.printStackTrace();
			log.error("激活邮件发送失败！");
		}
	}

	/**
	 * HTML邮件发送
	 * 
	 * @param sendTO
	 *            接收者邮箱地址
	 * @param url
	 *            激活链接地址
	 */
	public static void sendHtmlEmail(String sendTO, String usernname, String url) {
		try {
			HtmlEmail email = new HtmlEmail();
			email.setAuthentication(USER, PWD);
			email.setHostName(SMTP);
			email.addTo(sendTO);
			email.setFrom(SENDFROM);
			email.setSubject(SUBJECT);
			String str = HTML.replace("{name}", usernname);
			str = str.replace("{url}", url);
			email.setHtmlMsg(str);
			// email.setHtmlMsg(String.format(HTML,url, url));
			email.setTextMsg(String.format(MSG, url));
			email.setCharset("GBK");
			email.send();
			log.info("激活邮件发送成功 [TO:" + sendTO + "]");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("激活邮件发送失败！");
		}

	}

	public static void sendPasswordHtmlEmail(String sendTO, String usernname,
			String password) {
		try {
			HtmlEmail email = new HtmlEmail();
			email.setAuthentication(USER, PWD);
			email.setHostName(SMTP);
			email.addTo(sendTO);
			email.setFrom(SENDFROM);
			email.setSubject(SUBJECT);
			String str = PASSWORD_HTML.replace("{name}", usernname);
			str = str.replace("{password}", password);
			email.setHtmlMsg(str);
			// email.setHtmlMsg(String.format(HTML,url, url));
			email.setTextMsg(String.format(MSG, password));
			email.setCharset("GBK");
			email.send();
			log.info("激活邮件发送成功 [TO:" + sendTO + "]");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("激活邮件发送失败！");
		}

	}

	/**
	 * 邮件附件类型
	 * 
	 * @author 王维
	 * 
	 */
	public enum EmailAttachType {
		FILE, URL
	}
}
